<?php

// +-----------------------------------------------------------------------
// | @Copyright (c) 2012 http://t00ls.net.
// +-----------------------------------------------------------------------
// | @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-----------------------------------------------------------------------
// | @author: lenush <jnicklasj@gmail.com> qq:707207845
// +-----------------------------------------------------------------------

class TestsubjectAction extends CommonAction {
    
	function _filter(&$map){
        if(!empty($_POST['name'])) {
        $map['title'] = array('like',"%".$_POST['name']."%");
        }
    }

    function insert() {
    $name = $this->getActionName();
    $model = D($name);
    if (false === $model->create()) {
        $this->error($model->getError());
    }
	$option = '';
	$answer ='' ;
	if($_REQUEST['type'] == 1){
		if(!empty($_REQUEST['option1'])){
		    $option .= addslashes(trim($_REQUEST['option1'])).'|||';	
		}
		if(!empty($_REQUEST['option2'])){
		    $option .= addslashes(trim($_REQUEST['option2'])).'|||';	
		}
		if(!empty($_REQUEST['option3'])){
		    $option .= addslashes(trim($_REQUEST['option3'])).'|||';	
		}
		if(!empty($_REQUEST['option4'])){
		    $option .= addslashes(trim($_REQUEST['option4'])).'|||';	
		}
		$answer = $_REQUEST['answer'];
	}elseif($_REQUEST['type'] == 2){
		if(!empty($_REQUEST['moption1'])){
		    $option .= addslashes(trim($_REQUEST['moption1'])).'|||';	
		}
		if(!empty($_REQUEST['moption2'])){
		    $option .= addslashes(trim($_REQUEST['moption2'])).'|||';	
		}
		if(!empty($_REQUEST['moption3'])){
		    $option .= addslashes(trim($_REQUEST['moption3'])).'|||';	
		}
		if(!empty($_REQUEST['moption4'])){
		    $option .= addslashes(trim($_REQUEST['moption4'])).'|||';	
		}
		$answer = implode('|||',$_REQUEST['manswer']);
	}
        //保存当前数据对象
	$data = array(
		'type' => $_REQUEST['type'],
		'title' => addslashes(trim($_REQUEST['title'])),
		'option' => $option,
		'answer' => $answer,
	);
        $list = $model->add($data);
        if ($list !== false) { //保存成功
            $this->success('新增成功!',cookie('_currentUrl_'));
        } else {
            //失败提示
            $this->error('新增失败!');
        }
    }

    function edit() {
        $name = $this->getActionName();
        $model = M($name);
        $id = $_REQUEST [$model->getPk()];
        $vo = $model->getById($id);
        $optionArr = explode('|||',$vo['option']);
        unset($optionArr[4]);
        $vo['option'] = $optionArr;
        $vo['answer'] = explode('|||',$vo['answer']);
        $this->assign('answer',$vo['answer']);
        $this->assign('vo', $vo);
        $this->display();
    }
    
    public function update(){
    $name = $this->getActionName();
    $model = D($name);
    if (false === $model->create()) {
        $this->error($model->getError());
    }
	$option = '';
	$answer ='' ;
	if($_REQUEST['type'] == 1){
		if(!empty($_REQUEST['option1'])){
		    $option .= addslashes(trim($_REQUEST['option1'])).'|||';	
		}
		if(!empty($_REQUEST['option2'])){
		    $option .= addslashes(trim($_REQUEST['option2'])).'|||';	
		}
		if(!empty($_REQUEST['option3'])){
		    $option .= addslashes(trim($_REQUEST['option3'])).'|||';	
		}
		if(!empty($_REQUEST['option4'])){
		    $option .= addslashes(trim($_REQUEST['option4'])).'|||';	
		}
		$answer = $_REQUEST['answer'];
	}elseif($_REQUEST['type'] == 3){
		if(!empty($_REQUEST['option1'])){
		    $option .= addslashes(trim($_REQUEST['option1'])).'|||';	
		}
		if(!empty($_REQUEST['option2'])){
		    $option .= addslashes(trim($_REQUEST['option2'])).'|||';	
		}
		if(!empty($_REQUEST['option3'])){
		    $option .= addslashes(trim($_REQUEST['option3'])).'|||';	
		}
		if(!empty($_REQUEST['option4'])){
		    $option .= addslashes(trim($_REQUEST['option4'])).'|||';	
		}
		$answer = $_REQUEST['ranswer'];	
	}elseif($_REQUEST['type'] == 2){
		if(!empty($_REQUEST['moption1'])){
		    $option .= addslashes(trim($_REQUEST['moption1'])).'|||';	
		}
		if(!empty($_REQUEST['moption2'])){
		    $option .= addslashes(trim($_REQUEST['moption2'])).'|||';	
		}
		if(!empty($_REQUEST['moption3'])){
		    $option .= addslashes(trim($_REQUEST['moption3'])).'|||';	
		}
		if(!empty($_REQUEST['moption4'])){
		    $option .= addslashes(trim($_REQUEST['moption4'])).'|||';	
		}
		$answer = implode('|||',$_REQUEST['manswer']);
	}
        //保存当前数据对象
	$data = array(
        'id' => $_REQUEST['id'],
		'type' => $_REQUEST['type'],
		'title' => addslashes(trim($_REQUEST['title'])),
		'option' => $option,
		'answer' => $answer,
	);
        $list = $model->save($data);
        if ($list !== false) { //保存成功
            $this->success('编辑成功!',cookie('_currentUrl_'));
        } else {
            //失败提示
            $this->error('编辑失败!');
        }
    }

}
